//
//  WNCache.m
//  Whatnet_iOS
//
//  Created by Bill on 2019/4/22.
//  Copyright © 2019 什么网络. All rights reserved.
//

#import "WNCache.h"

@implementation WNCache
#pragma mark - 初始化
+ (id)shared
{
    return [[self alloc] init];
}

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static WNCache *instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [super allocWithZone:zone];
    });
    return instance;
}

+ (void)initialize
{
    [self creatCacheSubFolder:@"imageCaches"];
    [self creatCacheSubFolder:@"dataCaches"];
}

+ (NSString *)imageCachesFolder
{
    return [WNCaches stringByAppendingPathComponent:@"imageCaches"];
}

+ (NSString *)dataCachesFolder
{
    return [WNCaches stringByAppendingPathComponent:@"DataCaches"];
}

// 创建目录path
+ (BOOL)creatDirectoryPath:(NSString *)path
{
    // 1、初始化fileManager对象
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    // 2、如果存在目录直接返回，否则创建目标及中间目录
    BOOL isDir;
    BOOL existed = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    if (!(existed && isDir)){
        NSError *error = nil;
        BOOL result = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
        NSLog(@"目录%@创建成功!", path);
        if (error) NSLog(@"错误代码：%@", error.localizedDescription);
        return result;
    }else{
        NSLog(@"已存在目录%@...", path);
        return YES;
    }
}

// 保存图片
+ (void)saveImage:(UIImage *)image fileName:(NSString *)filename complete:(void (^)(BOOL success, NSString *filePath))block
{
    if (block == nil) return;
    NSString *folder = [self creatFolder:[NSString dateString] withPath:[self imageCachesFolder]];
    NSString *filePath = [folder stringByAppendingPathComponent:filename];
    BOOL result = [UIImageJPEGRepresentation(image, 0.5) writeToFile:filePath atomically:YES];
    if(result == NO)  block(NO, nil);
    if(result == YES) block(YES, filePath);
}

// 在path路径下创建目录folder
+ (NSString *)creatFolder:(NSString *)folder withPath:(NSString *)path
{
    NSString *newPath = [path stringByAppendingPathComponent:folder];
    if ([self creatDirectoryPath:newPath]){
        return newPath;
    }else{
        NSLog(@"目录创建失败，返回原路径");
        return path;
    }
}

// 在Documents中创建文件夹
+ (NSString *)creatDocSubFolder:(NSString *)folder
{
    return [self creatFolder:folder withPath:WNDocuments];
}

// 在Caches中创建文件夹
+ (NSString *)creatCacheSubFolder:(NSString *)folder
{
    return [self creatFolder:folder withPath:WNCaches];
}

#pragma mark 存储数据方法
// Foundation常规类
+ (void)saveValue:(id)value forKey:(NSString *)key
{
    if ([value isKindOfClass:[NSNull class]]) value = @"";
    [WNPrefe setValue:value forKey:key];
    [WNPrefe synchronize];
}
// NSInteger类型数据存储
+ (void)saveInteger:(NSInteger)value forKey:(NSString *)key
{
    [WNPrefe setInteger:value forKey:key];
    [WNPrefe synchronize];
}
// BOOL类型数据存储
+ (void)saveBool:(BOOL)value forKey:(NSString *)key
{
    [WNPrefe setBool:value forKey:key];
    [WNPrefe synchronize];
}
// Float类型数据存储
+ (void)saveFloat:(float)value forKey:(NSString *)key
{
    [WNPrefe setFloat:value forKey:key];
    [WNPrefe synchronize];
}
// Coor类型数据存储
+ (void)saveCoor:(CLLocationCoordinate2D)coor forKey:(NSString *)key
{
    NSString *latitude = [NSString stringWithFormat:@"%f", coor.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f", coor.longitude];
    NSDictionary *coorDict = @{@"latitude":latitude, @"longitude":longitude};
    [self saveValue:coorDict forKey:key];
}

#pragma mark 获取沙盒数据方法
+ (id)getValueForKey:(NSString *)key
{
    return [WNPrefe valueForKey:key];
}
+ (NSInteger)getIntegerForKey:(NSString *)key
{
    return [WNPrefe integerForKey:key];
}
+ (BOOL)getBoolForKey:(NSString *)key
{
    return [WNPrefe boolForKey:key];
}
+ (float)getFloatForKey:(NSString *)key
{
    return [WNPrefe floatForKey:key];
}
+ (CLLocationCoordinate2D)getCoorForKey:(NSString *)key
{
    NSDictionary *coorDict = [self getValueForKey:key];
    CLLocationDegrees latitude = [[coorDict sa_objectForKey:@"latitude"] doubleValue];
    CLLocationDegrees longitude = [[coorDict sa_objectForKey:@"longitude"] doubleValue];
    return CLLocationCoordinate2DMake(latitude, longitude);
}


@end
